package com.simple.apt.processor;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.google.auto.service.AutoService;
import com.simple.annotation.apt.Simpler;

import javax.annotation.processing.Processor;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FanXing
 * @date 2023年01月25日 0:35
 */
@SupportedAnnotationTypes({"com.simple.annotation.apt.Simpler"})
@AutoService(Processor.class)
public class ServiceImplProcessor extends BasicProcessor {
    private static final String PATTERN =
            "package {package};\n" +
            "\n" +
            "import com.simple.core.service.impl.ServiceImpl;\n" +
            "import org.springframework.stereotype.Service;\n" +
            "\n" +
            "@Service\n" +
            "public class {className} extends ServiceImpl<{entityClassName}, {daoName}> implements {serviceName} {\n" +
            "}";

    private String prefix;

    @Override
    protected String initBuilderClassSrc(TypeElement annotatedClass, String packageStr, String builderClassName, String entityClassName) {
        Map<String, String> map = new HashMap<>();
        map.put("package", packageStr);
        map.put("className", builderClassName);
        map.put("daoName", prefix + "Repository");
        map.put("entityClassName", entityClassName);
        map.put("serviceName", prefix + "Service");
        return StrFormatter.format(PATTERN, map, false);
    }

    @Override
    protected String initBuilderClassName(TypeElement annotatedClass, String className) {
        String actual = annotatedClass.getAnnotation(Simpler.class).prefix();
        prefix = className;
        if (StrUtil.isNotBlank(actual)) prefix = actual;
        return prefix + "ServiceImpl";
    }

}
